﻿#define _new

using System;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace EgeUniversity.SolarCarProject
{
	public class SolarCar : DependencyObject
	{
		//Pinarbasi GPS values. (Rectangle frame)
		public const double GPSLeft		= 27.270942;
		public const double GPSRight	= 27.27504;
		public const double GPSTop		= 38.435069;
		public const double GPSBottom	= 38.433657;

		public double MapWidth, MapHeight;

		#region Properties
		#region Dependency Properties
		public static DependencyProperty GPSLocationProperty			= DependencyProperty.Register("GPSLocation", typeof(Point), typeof(SolarCar));
		public static DependencyProperty GPSOnMapXProperty				= DependencyProperty.Register("GPSOnMapX", typeof(double), typeof(SolarCar));
		public static DependencyProperty GPSOnMapYProperty				= DependencyProperty.Register("GPSOnMapY", typeof(double), typeof(SolarCar));
		public static DependencyProperty AltitudeProperty				= DependencyProperty.Register("Altitude", typeof(double), typeof(SolarCar));
		public static DependencyProperty VelocityProperty				= DependencyProperty.Register("Velocity", typeof(double), typeof(SolarCar));
		public static DependencyProperty VelocityProperty2				= DependencyProperty.Register("Velocity2", typeof(double), typeof(SolarCar));

		public static DependencyProperty BatteryStateOfChargeProperty	= DependencyProperty.Register("BatteryStateOfCharge", typeof(double), typeof(SolarCar));
		public static DependencyProperty BatteryCurrentProperty			= DependencyProperty.Register("BatteryCurrent", typeof(double), typeof(SolarCar));
		public static DependencyProperty BatteryCurrentNegProperty		= DependencyProperty.Register("BatteryCurrentNeg", typeof(double), typeof(SolarCar));
		public static DependencyProperty BatteryVoltageProperty			= DependencyProperty.Register("BatteryVoltage", typeof(double), typeof(SolarCar));
		public static DependencyProperty BatteryTemperatureProperty		= DependencyProperty.Register("BatteryTemperature", typeof(double), typeof(SolarCar));

		public static DependencyProperty PVVoltageProperty1				= DependencyProperty.Register("PVVoltage1", typeof(double), typeof(SolarCar));
		public static DependencyProperty PVCurrentProperty1				= DependencyProperty.Register("PVCurrent1", typeof(double), typeof(SolarCar));
		public static DependencyProperty PVVoltageProperty2				= DependencyProperty.Register("PVVoltage2", typeof(double), typeof(SolarCar));
		public static DependencyProperty PVCurrentProperty2				= DependencyProperty.Register("PVCurrent2", typeof(double), typeof(SolarCar));
		public static DependencyProperty PVTemperatureProperty1			= DependencyProperty.Register("PVTemperature1", typeof(double), typeof(SolarCar));
		public static DependencyProperty PVTemperatureProperty2			= DependencyProperty.Register("PVTemperature2", typeof(double), typeof(SolarCar));
		public static DependencyProperty PVTemperatureProperty3			= DependencyProperty.Register("PVTemperature3", typeof(double), typeof(SolarCar));

		public static DependencyProperty MotorCurrentProperty			= DependencyProperty.Register("MotorCurrent", typeof(double), typeof(SolarCar));
		public static DependencyProperty MotorTemperatureProperty		= DependencyProperty.Register("MotorTemperature", typeof(double), typeof(SolarCar));
		#endregion
		#region Mirrors
		public Point GPSLocation
		{
			get { return (Point)GetValue(GPSLocationProperty); }
			set
			{
				SetValue(GPSLocationProperty, new Point(value.X, value.Y));
				SetValue(GPSOnMapXProperty, Calculations.Map(GPSLocation.X, GPSLeft, GPSRight, 0, MapWidth));
				SetValue(GPSOnMapYProperty, Calculations.Map(GPSLocation.Y, GPSBottom, GPSTop, 0, MapHeight));
			}
		}
		public double Altitude
		{
			get { return (double)GetValue(AltitudeProperty); }
			set { SetValue(AltitudeProperty, value); }
		}
		public double GPSOnMapX
		{
			get { return (double)GetValue(GPSOnMapXProperty); }
		}
		public double GPSOnMapY
		{
			get { return (double)GetValue(GPSOnMapYProperty); }
		}
		public double Velocity
		{
			get { return (double)GetValue(VelocityProperty); }
			set { SetValue(VelocityProperty, value); }
		}
		public double Velocity2
		{
			get { return (double)GetValue(VelocityProperty2); }
			set { SetValue(VelocityProperty2, value); }
		}
		public double BatteryStateOfCharge
		{
			get { return (double)GetValue(BatteryStateOfChargeProperty); }
			set { SetValue(BatteryStateOfChargeProperty, value); }
		}
		public double BatteryTemperature
		{
			get { return (double)GetValue(BatteryTemperatureProperty); }
			set { SetValue(BatteryTemperatureProperty, value); }
		}
		public double PVTemperature1
		{
			get { return (double)GetValue(PVTemperatureProperty1); }
			set { SetValue(PVTemperatureProperty1, value); }
		}
		public double PVTemperature2
		{
			get { return (double)GetValue(PVTemperatureProperty2); }
			set { SetValue(PVTemperatureProperty2, value); }
		}
		public double PVTemperature3
		{
			get { return (double)GetValue(PVTemperatureProperty3); }
			set { SetValue(PVTemperatureProperty3, value); }
		}
		public double PVVoltage1
		{
			get { return (double)GetValue(PVVoltageProperty1); }
			set { SetValue(PVVoltageProperty1, value); }
		}
		public double PVVoltage2
		{
			get { return (double)GetValue(PVVoltageProperty2); }
			set { SetValue(PVVoltageProperty2, value); }
		}
		public double PVCurrent1
		{
			get { return (double)GetValue(PVCurrentProperty1); }
			set { SetValue(PVCurrentProperty1, value); }
		}
		public double PVCurrent2
		{
			get { return (double)GetValue(PVCurrentProperty2); }
			set { SetValue(PVCurrentProperty2, value); }
		}
		public double BatteryVoltage
		{
			get { return (double)GetValue(BatteryVoltageProperty); }
			set { SetValue(BatteryVoltageProperty, value); }
		}
		public double BatteryCurrent
		{
			get
			{
				return (double)GetValue(BatteryCurrentProperty);
			}
			set
			{
				SetValue(BatteryCurrentProperty, value);
				SetValue(BatteryCurrentNegProperty, (value < 0) ? Math.Abs(value) : 0);
			}
		}
		public double MotorCurrent
		{
			get { return (double)GetValue(MotorCurrentProperty); }
			set { SetValue(MotorCurrentProperty, value); }
		}
		public double MotorTemperature
		{
			get { return (double)GetValue(MotorTemperatureProperty); }
			set { SetValue(MotorTemperatureProperty, value); }
		}
		#endregion
		#endregion

		public void SetCarBinding(FrameworkElement element, DependencyProperty property, string value)
		{
			//Data binding
			Binding binding = new Binding();
			binding.Source = this;
			binding.Path = new PropertyPath(value);
			binding.Mode = BindingMode.OneWay;
			element.SetBinding(property, binding);
		}

		public void SetCarValue(string str)
		{
			try
			{
				//Parse data and set variables
				string[] s = str.Split('#');
				double value;
#if _new
				if (Double.TryParse(s[0], out value))
					this.BatteryVoltage = value;
				if (Double.TryParse(s[1], out value))
					this.BatteryCurrent = value;
				if (Double.TryParse(s[2], out value))
					this.MotorCurrent = value;
				if (Double.TryParse(s[3], out value))
					this.MotorTemperature = value;
				if (Double.TryParse(s[4], out value))
					this.Velocity = value;
				//if (Double.TryParse(s[0], out value) &&
				//    Double.TryParse(s[1], out v2))
				//    this.GPSLocation = new Point(value, v2);
				//if (Double.TryParse(s[2], out value))
				//    this.Altitude = value;
				//if (Double.TryParse(s[3], out value))
				//    this.Velocity = value;
				//if (Double.TryParse(s[4], out value))
				//    this.Velocity2 = value;
				//if (Double.TryParse(s[5], out value))
				//    this.BatteryVoltage = value;
				//if (Double.TryParse(s[6], out value))
				//    this.BatteryCurrent = value;
				//if (Double.TryParse(s[7], out value))
				//    this.PVVoltage1 = value;
				//if (Double.TryParse(s[8], out value))
				//    this.PVCurrent1 = value;
				//if (Double.TryParse(s[9], out value))
				//    this.PVVoltage2 = value;
				//if (Double.TryParse(s[10], out value))
				//    this.PVCurrent2 = value;
				//if (Double.TryParse(s[13], out value))
				//    this.PVTemperature1 = value;
				//if (Double.TryParse(s[14], out value))
				//    this.PVTemperature2 = value;
				//if (Double.TryParse(s[15], out value))
				//    this.PVTemperature3 = value;
				
				//if (Double.TryParse(s[17], out value))
				//    this.BatteryStateOfCharge = value;
#endif
			}
			catch{}
		}
	}

	public enum CarValues
	{
		GPS_Latitude,
		GPS_Longitude,
		GPS_Altitude,
		Velocity,
		Velocity2,
		BatteryStateOfCharge,
		BatteryCurrent,
		BatteryVoltage,
		BatteryTemperature,
		PV_Voltage1,
		PV_Voltage2,
		PV_Current1,
		PV_Current2,
		PV_Temperature1,
		PV_Temperature2,
		PV_Temperature3
	}
}
